package com.aaie.gru.user.service.impl;

import com.aaie.gru.gruenum.UserType;
import com.aaie.gru.theme.entity.GruSysThemeConfig;
import com.aaie.gru.theme.service.IGruSysThemeConfigService;
import com.aaie.gru.user.entity.*;
import com.aaie.gru.user.mapper.GruSysUserMapper;
import com.aaie.gru.user.service.IGruSysGuardianSchoolMapService;
import com.aaie.gru.user.service.IGruSysUserService;
import com.aaie.gru.util.ConstantUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author lvxing
 * @since 2019-08-10
 */
@Service
@Transactional
public class GruSysUserServiceImpl extends ServiceImpl<GruSysUserMapper, GruSysUser> implements IGruSysUserService {

    @Resource
    GruSysUserMapper gruSysUserMapper;

    @Resource
    IGruSysGuardianSchoolMapService gruSysGuardianSchoolMapService;

    @Resource
    IGruSysThemeConfigService iGruSysThemeConfigService;

    @Resource
    IGruSysUserService iGruSysUserService;

    @Override
    public GruSysGuardian insertGruSysGuardian(GruSysGuardian gruSysGuardian) {
        // 转换对象 gruSysGuardian -> GruSysUser
        GruSysUser gruSysUser = new GruSysUser();
        // 因为要返回创建监护人的信息，所以不能在下方代码之后再对gruSysUser改变属性，否则返回的信息就不准确了
        BeanUtils.copyProperties(gruSysGuardian, gruSysUser);
        // 保存监护人
        boolean save = save(gruSysUser);
        // 处理监护人管理学校信息
        List<GruSysSchool> gruSysSchoolList = gruSysGuardian.getGruSysSchoolList();
        if (!CollectionUtils.isEmpty(gruSysSchoolList)) {
            List<GruSysGuardianSchoolMap> gruSysGuardianSchoolMapList = new ArrayList<>();
            for (GruSysSchool gruSysSchool : gruSysSchoolList) {
                GruSysGuardianSchoolMap gruSysGuardianSchoolMap = new GruSysGuardianSchoolMap();
                // 监护人id
                gruSysGuardianSchoolMap.setGuardianId(gruSysUser.getUserId());
                gruSysGuardianSchoolMap.setSchoolId(gruSysSchool.getUserId());
                gruSysGuardianSchoolMap.setCreateName(gruSysGuardian.getCreateName());
                gruSysGuardianSchoolMap.setCreateTime(gruSysGuardian.getCreateTime());
                gruSysGuardianSchoolMap.setCreateTimeSort(gruSysGuardian.getCreateTimeSort());
                gruSysGuardianSchoolMap.setUpdateName(gruSysGuardian.getUpdateName());
                gruSysGuardianSchoolMap.setUpdateTime(gruSysGuardian.getUpdateTime());
                gruSysGuardianSchoolMap.setUpdateTimeSort(gruSysGuardian.getUpdateTimeSort());
                gruSysGuardianSchoolMapList.add(gruSysGuardianSchoolMap);
            }
            if (!CollectionUtils.isEmpty(gruSysGuardianSchoolMapList)) {
                gruSysGuardianSchoolMapService.saveBatch(gruSysGuardianSchoolMapList);
            }
        }
        if (save) {
            gruSysGuardian.setUserId(gruSysUser.getUserId());
            return gruSysGuardian;
        }
        return null;
    }

    @Override
    public GruSysGuardian updateGruSysGuardian(GruSysGuardian gruSysGuardian) {
        // 转换对象 gruSysGuardian -> GruSysUser
        GruSysUser gruSysUser = new GruSysUser();
        BeanUtils.copyProperties(gruSysGuardian, gruSysUser);
        boolean b = updateById(gruSysUser);
        // 处理监护人管理学校信息
        List<GruSysSchool> gruSysSchoolList = gruSysGuardian.getGruSysSchoolList();
        if (!CollectionUtils.isEmpty(gruSysSchoolList)) {
            List<GruSysGuardianSchoolMap> gruSysGuardianSchoolMapList = new ArrayList<>();
            // 删除监护人管理学校信息
            QueryWrapper<GruSysGuardianSchoolMap> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("guardian_id", gruSysGuardian.getUserId());
            gruSysGuardianSchoolMapService.remove(queryWrapper);
            // 添加新增的监护人管理学校信息
            for (GruSysSchool gruSysSchool : gruSysSchoolList) {
                GruSysGuardianSchoolMap gruSysGuardianSchoolMap = new GruSysGuardianSchoolMap();
                // 监护人id
                gruSysGuardianSchoolMap.setGuardianId(gruSysUser.getUserId());
                gruSysGuardianSchoolMap.setSchoolId(gruSysSchool.getUserId());
                gruSysGuardianSchoolMap.setCreateName(gruSysGuardian.getCreateName());
                gruSysGuardianSchoolMap.setCreateTime(gruSysGuardian.getCreateTime());
                gruSysGuardianSchoolMap.setCreateTimeSort(gruSysGuardian.getCreateTimeSort());
                gruSysGuardianSchoolMap.setUpdateName(gruSysGuardian.getUpdateName());
                gruSysGuardianSchoolMap.setUpdateTime(gruSysGuardian.getUpdateTime());
                gruSysGuardianSchoolMap.setUpdateTimeSort(gruSysGuardian.getUpdateTimeSort());
                gruSysGuardianSchoolMapList.add(gruSysGuardianSchoolMap);
            }
            if (!CollectionUtils.isEmpty(gruSysGuardianSchoolMapList)) {
                gruSysGuardianSchoolMapService.saveBatch(gruSysGuardianSchoolMapList);
            }
        }
        if (b) {
            return gruSysGuardian;
        }
        return null;
    }

    @Override
    public GruSysParent insertGruSysParent(GruSysParent gruSysParent) {
        // 转换对象 gruSysParent -> GruSysUser
        GruSysUser gruSysUser = new GruSysUser();
        // 因为要返回创建家长的信息，所以不能在下方代码之后再对gruSysUser改变属性，否则返回的信息就不准确了
        BeanUtils.copyProperties(gruSysParent, gruSysUser);
        boolean save = save(gruSysUser);
        if (save) {
            gruSysParent.setUserId(gruSysUser.getUserId());
            return gruSysParent;
        }
        return null;
    }

    @Override
    public GruSysParent updateGruSysParent(GruSysParent gruSysParent) {
        // 转换对象 gruSysParent -> GruSysUser
        GruSysUser gruSysUser = new GruSysUser();
        BeanUtils.copyProperties(gruSysParent, gruSysUser);
        boolean b = updateById(gruSysUser);
        if (b) {
            return gruSysParent;
        }
        return null;
    }

    @Override
    public GruSysSchool insertGruSysSchool(GruSysSchool gruSysSchool) {
        GruSysUser gruSysUser = new GruSysUser();
        BeanUtils.copyProperties(gruSysSchool, gruSysUser);
        boolean save = save(gruSysUser);
        if (save) {
            gruSysSchool.setUserId(gruSysUser.getUserId());
            return gruSysSchool;
        }
        return null;
    }

    @Override
    public GruSysSchool updateGruSysSchool(GruSysSchool gruSysSchool) {
        GruSysUser gruSysUser = new GruSysUser();
        BeanUtils.copyProperties(gruSysSchool, gruSysUser);
        boolean b = updateById(gruSysUser);
        if (b) {
            return gruSysSchool;
        }
        return null;
    }

    @Override
    public GruSysStaff insertGruSysStaff(GruSysStaff gruSysStaff) {
        GruSysUser gruSysUser = new GruSysUser();
        BeanUtils.copyProperties(gruSysStaff, gruSysUser);
        boolean save = save(gruSysUser);
        if (save) {
            gruSysStaff.setUserId(gruSysUser.getUserId());
            return gruSysStaff;
        }
        return null;
    }

    @Override
    public GruSysStaff updateGruSysStaff(GruSysStaff gruSysStaff) {
        GruSysUser gruSysUser = new GruSysUser();
        BeanUtils.copyProperties(gruSysStaff, gruSysUser);
        boolean b = updateById(gruSysUser);
        if (b) {
            return gruSysStaff;
        }
        return null;
    }

    @Override
    public GruSysStu insertGruSysStu(GruSysStu gruSysStu) {
        GruSysUser gruSysUser = new GruSysUser();
        BeanUtils.copyProperties(gruSysStu, gruSysUser);
        boolean save = save(gruSysUser);
        if (save) {
            gruSysStu.setUserId(gruSysUser.getUserId());
            return gruSysStu;
        }
        return null;
    }

    @Override
    public GruSysStu updateGruSysStu(GruSysStu gruSysStu) {
        GruSysUser gruSysUser = new GruSysUser();
        BeanUtils.copyProperties(gruSysStu, gruSysUser);
        GruSysUser gruSysUserTemp = iGruSysUserService.getById(gruSysStu.getUserId());
        if (StringUtils.isEmpty(gruSysUserTemp.getStuFile1Path())) {

        }
        boolean b = updateById(gruSysUser);
        if (b) {
            return gruSysStu;
        }
        return null;
    }

    @Override
    public IPage<GruSysStuVo> getGruSysStuList(GruSysStuDto param) {
        // 定义分页
        Page<GruSysStuVo> page = new Page<>(param.getCurrent(), param.getSize());
        // 查询条件
        QueryWrapper<GruSysStuVo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getSearch())) {
            queryWrapper.and(qw -> qw.like("stu.user_name", param.getSearch())
                    .or().like("stu.user_account", param.getSearch()));
        }

        if(StringUtils.isNotEmpty(param.getStuGurdianId())){
            queryWrapper.eq(StringUtils.isNotEmpty(param.getStuGurdianId()), "stu.stu_gurdian_id", param.getStuGurdianId());
        }
        // 类型：监护人
        queryWrapper.eq("stu.user_type", UserType.STUDENT.getValue());
        queryWrapper.eq("stu.del_flag", "0");
        // 按更新时间倒叙排序
        queryWrapper.orderByDesc("stu.update_time_sort");
        return gruSysUserMapper.selectGruSysStuVoList(page, queryWrapper);
    }

    @Override
    public IPage<GruSysUser> getGruSysGurdianList(GruSysStuDto param) {
        // 定义分页
        Page<GruSysStuVo> page = new Page<>(param.getCurrent(), param.getSize());
        // 查询条件
        QueryWrapper<GruSysStuVo> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotEmpty(param.getStuFatherId()) || StringUtils.isNotEmpty(param.getStuMotherId())){
            //queryWrapper.and(qw -> qw.eq("b.stu_father_id", param.getStuFatherId()).or().eq("b.stu_mother_id", param.getStuMotherId()));
            queryWrapper.and(qw -> qw.eq("a.stu_father_id", param.getStuFatherId()));
        }

        // 按更新时间倒叙排序
        queryWrapper.orderByDesc("b.update_time_sort");
        IPage<GruSysUser> pageList = gruSysUserMapper.selectGurdianList(page, queryWrapper);
        List<GruSysUser> gruSysUserList = pageList.getRecords();
        List<GruSysUser> list = new ArrayList<>();
        QueryWrapper<GruSysThemeConfig> queryWrapperGrySysThemeConfig = new QueryWrapper<>();
        queryWrapperGrySysThemeConfig.eq("theme_id",param.getStuFatherId());
        queryWrapperGrySysThemeConfig.eq("current_user_id",param.getCurrentUserId());
        queryWrapperGrySysThemeConfig.eq("theme_new",ConstantUtil.THEME_NEW_Y);
        int count = iGruSysThemeConfigService.count(queryWrapperGrySysThemeConfig);
        for(int i = 0;i < gruSysUserList.size();i ++){
            GruSysUser gruSysUser = gruSysUserList.get(i);
            if(count > 0){
                gruSysUser.setIsNewMes(ConstantUtil.THEME_NEW_Y);
            }else{
                gruSysUser.setIsNewMes(ConstantUtil.THEME_NEW_N);
            }

            list.add(gruSysUser);
        }

        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<GruSysUser> getGruSysStuList(QueryWrapper<GruSysUser> queryWrapper) {
        List<GruSysUser> listStu = gruSysUserMapper.selectList(queryWrapper);
        return listStu;
    }
}
